package com.aaa.service.xg.impl;

import com.aaa.dao.xg.ViewResultDao;
import com.aaa.pojo.ViewResultFpa;
import com.aaa.service.xg.ViewResultFpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ViewResultPfaServiceImpl implements ViewResultFpaService {
    @Autowired
    private ViewResultDao viewResultDao;
    @Override
    public List<ViewResultFpa> findAll(String page,String limit) {
        int parseInt = Integer.parseInt(page);
        int anInt = Integer.parseInt(limit);
        int start = (parseInt-1)*anInt;
        return viewResultDao.findAll(start,anInt);
    }

    @Override
    public Map<String, Integer> findAllData() {
        List<ViewResultFpa> viewResultFpa = viewResultDao.findAllData();


        //   List<ViewTestResultFpa> viewTestResultFpas = this.viewTestResultFpaDao.queryAllPlanFpa();
        int r = 0;
        int y = 0;
        int b = 0;
        int g = 0;
        int f = 0;
        Iterator<ViewResultFpa> data = viewResultFpa.iterator();
        //  Iterator var7 = viewTestResultFpas.iterator();

        while (true) {
            while (data.hasNext()) {
                ViewResultFpa viewTestResultFpa = (ViewResultFpa) data.next();
                double red = viewTestResultFpa.getRedCount();
                double yellow = viewTestResultFpa.getYellowCount();
                double blue = viewTestResultFpa.getBlueCount();
                double green = viewTestResultFpa.getGreenCount();
                if (red > yellow && red > blue && red > green) {
                    ++r;
                } else if (yellow > red && yellow > blue && yellow > green) {
                    ++y;
                } else if (blue > red && blue > yellow && blue > green) {
                    ++b;
                } else if (green > red && green > yellow && green > blue) {
                    ++g;
                } else {
                    ++f;
                }
            }

            Map<String, Integer> map = new HashMap();
            map.put("r", r);
            map.put("y", y);
            map.put("b", b);
            map.put("g", g);
            map.put("f", f);
            return map;

        }
    }

    @Override
    public int findCount() {
        return viewResultDao.findCount();
    }

    @Override
    public int delete(List<String> testerId) {
        return viewResultDao.delete(testerId);
    }

    @Override
    public ViewResultFpa findById(int testerId) {
        return viewResultDao.findById(testerId);
    }

    @Override
    public List<ViewResultFpa> findCountById() {
        return viewResultDao.findCountPlanId();
    }

    @Override
    public List<ViewResultFpa> findIf(String name, String testPlanId, String page, String limit) {


        int parseInt = 0;
        if(testPlanId != null && testPlanId != ""){
            parseInt = Integer.parseInt(testPlanId);
        }else {
            parseInt = 0;
        }

        //int parseInt = Integer.parseInt(testPlanId);
        int parsePage = Integer.parseInt(page);
        int parseLimit = Integer.parseInt(limit);
        int start = (parsePage - 1)*parseLimit;

        List<ViewResultFpa> anIf = viewResultDao.findIf(name, parseInt, start, parseLimit);
        return anIf;

    }

    @Override
    public int findIfCount(String name, String testPlanId) {
        int parseInt = Integer.parseInt(testPlanId);
        int ifCount = viewResultDao.findIfCount(name, parseInt);
        return ifCount;
    }
}
